# 	Filename: all_driver.R
# 	Description: Driver script to produce the multiply imputed data set and all analyses in the manuscript and appendix.
# 	Author: Barry Hashimoto (bh84@nyu.edu)
# 	Date: December 2019
# 	For: Barry Hashimoto, "Autocratic Consent to International Law: the Case of the International Criminal Court's Jurisdiction," International Organization.

#   General Notes:
#   1. Run this driver script on Mac OS running R v. 3.6.0 or newer.
#	   - Source scripts use forking for imputation and estimation via library(parallel).
#	   - This may not work on R for a computer running Windows.
#   2. XQuartz must be installed on Mac OS.
#   3. Output for tables and other statistics will be printed to the R Console. 
#	   - Output is annotated, but not in the order or exact format as it appears in the article and appendix.
#   4. RNG seed is 100 throughout. R v. 3.6.0 and newer defaults are specified. 
#   5. System runtime on a 2018 Macbook Pro with a 2.6 Ghz 6-core Intel Core i7 and 16 GB 2400 MhZ DDR4...
#      is about 45 minutes for script code up to the "overimputationplot_source.R" script, and
#      is about 120 minutes for the "overimputationplot_source.R" script.

# Set the user's working directory, set the random number generator type to default, and clear objects.
setwd("/Users/barryhashimoto/Desktop/HashimotoReplicationFile2019") # Only instance across all driver and source scripts.
RNGkind(kind = "Mersenne-Twister", normal.kind = "Inversion", sample.kind = "Rejection") # Only instance across all driver and source scripts.
rm(list = ls())

# Install and update packages on the user's system if necessary.
packages <- c("foreign", "plyr", "dplyr", "doBy", "caret", "data.table", "mitools", "lattice", "psych", "hexbin", "mvtnorm","mvnfast", "cem", "MatchIt", "Zelig", "Amelia", "brglm", "AICcmodavg", "plm", "mgcv", "car", "arm", "xtable", "memisc", "parallel", "future")

if (length(setdiff(packages, rownames(installed.packages()))) > 0) {install.packages(setdiff(packages, rownames(installed.packages())), repos = "http://cran.us.r-project.org")}

# Source the script to produce the multiply imputed data and the overimputation plot in the appendix. This will print results to the R Console and save  files named MIforPlot.RData and imputationsicc.RData to your working directory.
SaveData <- TRUE # If TRUE then any file named "imputedicc.RData" will be overwritten.
source("imputation_source.R", spaced = T)

# Source the script to produce the summary statistics table in the appendix.
source("summarystatistics_source.R", spaced = T)

# Source driver scripts for all three dependent variables to produce all of the regression output in the manuscript and appendix. 
source("jurisdiction_driver.R", spaced = T)
source("exit_driver.R", spaced = T)
source("logdeaths_driver.R", spaced = T)

# Source the script to produce the overimputation plot to assess imputation accuracy, shown in the appendix. This will save a file named composite811.pdf to your working directory. This source script is computationally intensive.
source("overimputationplot_source.R", spaced = T)

sessionInfo()